﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TJBHJJ.Models;

namespace TJBHJJ.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/
        Home home = new Home();
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }
        #region 登录
        public ActionResult LogOn()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LogOn(Admin model,string returnUrl)
        {
            //这里缺少验证
            //需要写在这里
            //if (home.Admins.Where(x => x.UserName == model.UserName && x.PassWord == model.PassWord).Count() >= 1)
            //{
            //FormsAuthentication.SetAuthCookie(model.UserName, false);
            FormsAuthentication.SetAuthCookie("Test", false);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Manager");
                }
            //}
            //else
            //{
            //    return RedirectToAction("Logon", "Manager");
            //}
        }
        [Authorize]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Manager");
        }
        #endregion

        #region 新闻
        //新闻
        public ActionResult News()
        {
            return View(new News());
            //if (Request.IsAjaxRequest())
            //{
            //    return PartialView("News", new News());
            //}
            //else
            //{
            //    return View(new News());
            //}
        }

        [HttpPost]
        [Authorize]
        public ActionResult News(News news)
        {
            if (ModelState.IsValid)
            {
                news.GuidGid = Guid.NewGuid().ToString();
                news.CreatTime = DateTime.Now;
                news.ClassId = 0;
                home.DbInfo.AddToNews(news);
                home.DbInfo.SaveChanges();
                home.Alert(Response, "添加成功");
            }
            return RedirectToAction("NewList");

        }
        [Authorize]
        public ActionResult NewEdit(int page, int id)
        {
            //ViewData["pageCount"] = home.News.Count / home.PageSize;
            ViewData["pageIndex"] = page;
            return View(home.News.Where(x => x.Id == id).FirstOrDefault());
        }
        [HttpPost]
        [Authorize]
        [ValidateInput(false)]
        public ActionResult NewEdit(int page,News news)
        {
            if (ModelState.IsValid)
            {
                news.CreatTime = DateTime.Now;
                home.DbInfo.News.Attach(news);
                home.DbInfo.ObjectStateManager.ChangeObjectState(news, System.Data.EntityState.Modified);
                home.DbInfo.SaveChanges();
                home.Alert(Response, "修改成功");
            }
            //ViewData["pageCount"] = home.News.Count / home.PageSize;
            ViewData["pageIndex"] = page;
            //return View("NewList", home.Page<News>(home.News, page));
            return RedirectToAction("NewList", new { page = page });
        }
        [Authorize]
        public ActionResult NewList(int page = 0)
        {
            ViewData["pageCount"] = home.News.Count / home.PageSize;
            ViewData["pageIndex"] = page;
            return View(home.Page<News>(home.News, page));

            //if (Request.IsAjaxRequest())
            //{
            //    return PartialView("NewList", home.Page<News>(home.News, page));
            //}
            //else
            //{
            //    return View(home.Page<News>(home.News, page));
            //}
        }
        [Authorize]
        public ActionResult NewDelete(int page, int id)
        {
            News n = home.News.Where(x => x.Id == id).SingleOrDefault();
            if (n != null)
            {
                home.DbInfo.DeleteObject(n); 
                home.DbInfo.SaveChanges();
            }
            return RedirectToAction("NewList", new { page = page });
        }
        #endregion

        #region 添加产品
        public ActionResult ListProduct(int page=0)
        {
            ViewData["pageCount"] = home.Products.Count / home.PageSize;
            ViewData["pageIndex"] = page;
            return View(home.Page<Product>(home.Products, page));
        }
        [Authorize]
        public ActionResult AddProduct()
        {
            return View(new Product());
        }
        [Authorize]
        [HttpPost]
        public ActionResult AddProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                product.GuidId = Guid.NewGuid().ToString();
                product.CreatDateTime = DateTime.Now;
                product.ClassId = 0;
                product.UserId = 0;
                home.DbInfo.AddToProduct(product);
                home.DbInfo.SaveChanges();
                home.Alert(Response, "添加成功");
            }
            return RedirectToAction("ListProduct");
        }
        public ActionResult EditProduct(int page,int id)
        {
            //ViewData["pageCount"] = home.News.Count / home.PageSize;
            ViewData["pageIndex"] = page;
            return View(home.Products.Where(x => x.Id == id).FirstOrDefault());

        }

        [Authorize]
        [HttpPost]
        public ActionResult EditProduct(int page,Product product)
        {
            if (ModelState.IsValid)
            {
                product.CreatDateTime = DateTime.Now;
                home.DbInfo.Product.Attach(product);
                home.DbInfo.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Modified);
                home.DbInfo.SaveChanges();
                home.Alert(Response, "修改成功");
            }
            //ViewData["pageCount"] = home.News.Count / home.PageSize;
            ViewData["pageIndex"] = page;
            return RedirectToAction("ListProduct", new { page = page });

        }
        [Authorize]
        public ActionResult DeleteProduct(int page,int id)
        {
            Product n = home.Products.Where(x => x.Id == id).SingleOrDefault();
            if (n != null)
            {
                home.DbInfo.DeleteObject(n);
                home.DbInfo.SaveChanges();
            }
            return RedirectToAction("ListProduct", new { page = page });
        }
#endregion
        //添加招聘信息
        public ActionResult Job()
        {
            return null;
        }
        [HttpPost]
        public ActionResult Job(Job job)
        {
            return null;
        }
        //添加资料下载
        public ActionResult Down()
        {
            return null;
        }
        [HttpPost]
        public ActionResult Down(Down down)
        {
            return null;
        }
        //查看及回复留言
        public ActionResult Message()
        {
            return null;
        }
        [HttpPost]
        public ActionResult Message(Message msg)
        {
            return null;
        }
        //编辑网站配置信息
        public ActionResult WebConfig()
        {
            return null;
        }
        [HttpPost]
        public ActionResult WebConfig(WebConfig config)
        {
            return null;
        }
        //编辑系统信息
        public ActionResult SysConfig()
        {
            return null;
        }
        [HttpPost]
        public ActionResult SysConfig(SysConfig config)
        {
            return null;
        }
    }
}
